package com.example.voicealarm;

import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class AlarmPreferences {
    private static final String PREF_NAME = "AlarmPreferences";
    private static final String KEY_ALARMS = "alarms";
    private final SharedPreferences preferences;
    private final Gson gson;

    public AlarmPreferences(Context context) {
        preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        gson = new Gson();
    }

    public void saveAlarms(List<Alarm> alarms) {
        String json = gson.toJson(alarms);
        preferences.edit().putString(KEY_ALARMS, json).apply();
    }

    public List<Alarm> loadAlarms() {
        String json = preferences.getString(KEY_ALARMS, null);
        if (json == null) {
            return new ArrayList<>();
        }
        Type type = new TypeToken<List<Alarm>>(){}.getType();
        return gson.fromJson(json, type);
    }
}
